/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();

/**
 * 根据types查询记录
 */
var listByType = function (req, res) {
    var params = {};
    var paramT = {};
    try {
        paramT.attributes = ['codeId', 'codeDesc'];
        if (req.params.types != undefined) {
            params.types = parseInt(req.params.types);
        } else {
            res.json({
                "success": false,
                "msg": "空参错误！"
            });
        }
        paramT.where = params;
        paramT.order = [['num', 'asc']];
        seq.dictionaryModule.findAll(paramT).then(function (result) {
            res.json({
                "success": true,
                "msg": "查询成功！",
                "result": result
            });
        })
    } catch (err) {
        res.status(500).json({
            message: "服务错误！",
            err: err
        });
    }
}

exports.listByType = listByType;

/**
 * 新增字典表
 * @param {*} req 
 * @param {*} res 
 */
var create = function (req, res) {
    try {
        var uuid = util.getUUID();
        req.body.id = uuid;
        seq.dictionaryModule.create(req.body).then(function (result) {

            res.status(200).json({
                message: "添加字典表",
                result: result
            });
        })
    } catch (err) {
        res.status(500).json({
            message: "服务错误！",
            err: err
        });
    }
}
exports.create = create;

/**
 * 新增行业信息
 * @param {*} req
 * @param {*} res
 */
var addForhangye = function (req, res) {
    var params = {};
    var paramT = {};
    try {
        if (req.body.types != undefined) {
            params.types = req.body.types;
        }
        paramT.where = params;
        paramT.order = [['num', 'desc']];
        seq.dictionaryModule.findAndCountAll(paramT).then(function (results) {
            req.body.num = results.rows[0].num + 1;
            req.body.codeId = results.rows[0].codeId + 1;
            req.body.statu = 10011001;
            req.body.typeName = results.rows[0].typeName;
            seq.dictionaryModule.create(req.body).then(function (result) {
                res.status(200).json({
                    message: "添加成功",
                    result: result
                });
            })
        })

    } catch (err) {
        res.status(500).json({
            message: "服务错误！",
            err: err
        });
    }
}
exports.addForhangye = addForhangye;



/**
 * 查询字典表信息
 * @param {*} req
 * @param {*} res
 */

// var listByType = function (req, res) {
//     var params = {};
//     var paramT = {};
//     try {

//         if (req.params.types != undefined) {
//             params.types = req.params.types;
//         } else {
//             res.status(500).json({
//                 message: "空参错误！",
//             });
//         }
//         params.statu = 10011001;
//         paramT.where = params;
//         paramT.order = [['num', 'asc']];

//         seq.dictionaryModule.findAndCountAll(paramT).then(function (result) {
//             res.status(200).json({
//                 message: "查询字典",
//                 total: result.count,
//                 result: result.rows
//             });
//         })
//     } catch (err) {
//         res.status(500).json({
//             message: "服务错误！",
//             err: err
//         });
//     }
// }

// exports.listByType = listByType;

/**
 * 查询行业字典信息
 * @param {*} req
 * @param {*} res
 */

var listForhangye = function (req, res) {
    var params = {};
    var paramT = {};
    var page = req.query.page;
    var rows = req.query.rows;
    try {

        if (req.query.types != undefined) {
            params.types = req.query.types;
        } else {
            res.status(500).json({
                message: "空参错误！",
            });
        }

        if (req.query.codeDesc != undefined) {
            params.codeDesc = { "$like": "%" + req.query.codeDesc + "%" }
        }
        params.statu = 10011001;
        paramT.where = params;
        paramT.order = [['num', 'asc']];
        if (page != undefined && rows != undefined) {
            paramT.offset = parseInt(page);
            paramT.limit = parseInt(rows);
        }
        seq.dictionaryModule.findAndCountAll(paramT).then(function (result) {
            res.status(200).json({
                message: "查询字典",
                total: result.count,
                result: result.rows,
                page: page
            });
        })
    } catch (err) {
        res.status(500).json({
            message: "服务错误！",
            err: err
        });
    }
}

exports.listForhangye = listForhangye;

/**
 * 更新字典表信息
 * @param {*} req 
 * @param {*} res 
 */
var update = function (req, res) {
    try {
        if (req.params.ID != null && req.params.ID != "" && req.params.ID != undefined) {
            var updateinfo = {
                "codeDesc": req.body.codeDesc
            };
            seq.dictionaryModule.update(
                updateinfo,
                {
                    where: {
                        'id': req.params.ID
                    }
                }
            ).then(function (result) {
                res.status(200).json({
                    message: "更新信息成功",
                    result: result
                })
            })
        }
        // Dictionary.update(req.body,req.params).then(function(result){
        //      SysLog.Log(req, "更新字典表信息",req.session.user);
        //     res.status(200).json({
        //         message: "更新字典表信息",
        //         result:result
        //     });
        // })

    } catch (err) {
        res.status(500).json({
            message: "服务错误！",
            err: err
        });
    }
}


exports.update = update;

/**
 * 根据 ID 删除角色
 * @param {*} req
 * @param {*} res
 */
var deleteByID = function (req, res) {
    var arr = req.params.ID.split(",");
    var updateinfo = {
        "statu": 10011002
    };
    seq.dictionaryModule.update(
        updateinfo,
        {
            where: {
                'id': [arr]
            }

        }).then(function (result) {
            SysLog.Log(req, "删除字典", req.session.user);
            res.status(200).json({
                message: "删除信息成功",
                result: result
            })
        })
}

exports.deleteByID = deleteByID;

